package com.wannengbang.cloudleader.mine;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.BaseResponseBean;
import com.wannengbang.cloudleader.event.OrderRefushEvent;
import com.wannengbang.cloudleader.homepage.model.HomePageModelImpl;
import com.wannengbang.cloudleader.homepage.model.IHomePageModel;
import com.wannengbang.cloudleader.utils.ToastUtil;
import com.wannengbang.cloudleader.widget.AppTitleBar;
import com.wannengbang.cloudleader.widget.ViewLoading;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SendDeliveryActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.edit_delivery_no)
    EditText editDeliveryNo;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_delivery_type)
    TextView tvDeliveryType;
    @BindView(R.id.ll_delivery_type)
    LinearLayout llDeliveryType;
    private IHomePageModel homePageModel;
    private String shop_order_id;
    private ArrayList<String> optionsItems = new ArrayList<>();
    private int delivery_type;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_delivery);
        ButterKnife.bind(this);

        type = getIntent().getStringExtra("type");

        homePageModel = new HomePageModelImpl();
        shop_order_id = getIntent().getStringExtra("shop_order_id");
        optionsItems.add("顺丰速运");
        optionsItems.add("圆通速递");
        optionsItems.add("中通快递");
        optionsItems.add("申通快递");
        optionsItems.add("韵达速递");
        optionsItems.add("邮政包裹");
        optionsItems.add("德邦快递");
        optionsItems.add("百世快递");
        optionsItems.add("天天快递");
    }

    private void showGsInfo() {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerBuilder(mActivity, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                delivery_type = (options1 + 1);
                tvDeliveryType.setText(optionsItems.get(options1));
            }
        }).setTitleText("")
                .setTitleSize(16)
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(16)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(18)
                .setLineSpacingMultiplier(2)
                .isDialog(true)
                .build();


        pvOptions.setPicker(optionsItems);

        String deviceName = tvDeliveryType.getText().toString();

        if ("顺丰速运".equals(deviceName)) {
            pvOptions.setSelectOptions(0);
        } else if ("圆通速递".equals(deviceName)) {
            pvOptions.setSelectOptions(1);
        } else if ("中通快递".equals(deviceName)) {
            pvOptions.setSelectOptions(2);
        } else if ("申通快递".equals(deviceName)) {
            pvOptions.setSelectOptions(3);
        } else if ("韵达速递".equals(deviceName)) {
            pvOptions.setSelectOptions(4);
        } else if ("邮政包裹".equals(deviceName)) {
            pvOptions.setSelectOptions(5);
        } else if ("德邦快递".equals(deviceName)) {
            pvOptions.setSelectOptions(6);
        } else if ("百世快递".equals(deviceName)) {
            pvOptions.setSelectOptions(7);
        } else if ("天天快递".equals(deviceName)) {
            pvOptions.setSelectOptions(8);
        }

        Dialog mDialog = pvOptions.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvOptions.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvOptions.show();

    }

    @OnClick({R.id.ll_delivery_type, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_delivery_type:
                showGsInfo();
                break;
            case R.id.tv_commit:
                if (delivery_type == 0) {
                    ToastUtil.showShort("请选择快递公司");
                    return;
                }
                String delivery_no = editDeliveryNo.getText().toString();
                if (TextUtils.isEmpty(delivery_no)) {
                    ToastUtil.showShort("请输入快递单号");
                    return;
                }
                ViewLoading.showProgress(mActivity, "提交中......");
                if (!TextUtils.isEmpty(type)) {
                    homePageModel.requestStoreDeliveryShip(shop_order_id, String.valueOf(delivery_type), delivery_no, new DataCallBack<BaseResponseBean>() {
                        @Override
                        public void onSuccessful(BaseResponseBean bean) {
                            ToastUtil.showShort(bean.getMsg());
                            EventBus.getDefault().post(new OrderRefushEvent());
                            finish();
                        }

                        @Override
                        public void onFailed(String msg, String code) {

                        }
                    });
                } else {
                    homePageModel.requestStoreDelivery(shop_order_id, String.valueOf(delivery_type), delivery_no, new DataCallBack<BaseResponseBean>() {
                        @Override
                        public void onSuccessful(BaseResponseBean bean) {
                            ToastUtil.showShort(bean.getMsg());
                            EventBus.getDefault().post(new OrderRefushEvent());
                            finish();
                        }

                        @Override
                        public void onFailed(String msg, String code) {

                        }
                    });
                }

                break;
        }
    }
}
